from xml.dom import minidom
import urllib2
import urllib
import md5
from elementtree import ElementTree

def parse_file(xmlfile):
    "Returns the version number and a list of files."
    
    xmldoc = ElementTree.parse(xmlfile).getroot()
    
    version = xmldoc.get("version")
    files = []
    for x in xmldoc.getchildren():
        name = x[0].text
        checksum = x[1].text
        
        files.append([name, checksum])
        
    return [version, files]
    
def download_file(file_info):
    "Downloads and verifies a file. Returns None if error occurs."
    
    filename = file_info[0]
    checksum = file_info[1]
    
    downloaded_file = urllib.urlretrieve("http://updates.bethesdatech.net/renamer/" + filename)
    
    f = file(downloaded_file[0], 'rb')
    
    downloaded_checksum = md5.new()
    
    while True:
        part = f.read(1024)
        
        if not part:
            break
        
        downloaded_checksum.update(part)
    
    if checksum == downloaded_checksum.hexdigest():
        return downloaded_file
    
    else:
        return [filename, downloaded_file]
    

    